﻿using Nemerle.Collections;
using Nemerle.Text;
using Nemerle.Utility;

using System;
using System.Collections.Generic;
using System.Linq;
using NRails.Migrations;
using NRails.Macros.Migration;
using BLToolkit.Data.Linq;
using BLToolkit.Data.DataProvider;

namespace NRails.Demo.Migrations
{
  public class TestMigration : Migration
  {
          public this () {base("000000")}
          
          protected override Up() : void
          {
             create Person
             {
                 PersonID : int(pk, identity(1, 1));
                 FirstName : string(50);
                 LastName : string(50);
                 MiddleName : string?(50);
                 Gender : char; // todo : длину у char явно указывать не нужно
             }
              
             create Doctor
             {
                 PersonID :> Person(pk); 
                 Taxonomy : string(50);
             }
             
             create Patient
             {
                 PersonID :> Person;
                 Diagnosis : string(256);
             }

             // 1st insert way
             sql (db, "insert into Person (FirstName, LastName, Gender) values ('1', '2', 'M')") returning tt;
             Console.WriteLine($"inserted $tt records");
             
             // 2nd insert way
             for (mutable i = 1; i < 10; i++)
                insert into Person (FirstName, LastName, Gender) values (i.ToString(), "3", 'F');

             // 3rd insert way, with parameters
             for (mutable i = 1; i < 10; i++)
                sql(db, "insert into Person (FirstName, LastName, Gender) values (?, ?, ?)", $"f$i", $"l$i", 'M');
             
             // select sample
             select (db, "select $FirstName, $LastName, $(Gender : char) from Person where Gender=?", 'F')
             {
                 Console.WriteLine($"$FirstName\t$LastName\t$Gender");
             };
             
             // remove dml sample data
             //sql(db, "delete from Person"); // commented because fails next migration
            
/*
CREATE TABLE Person
(
	PersonID   int          NOT NULL IDENTITY(1,1) CONSTRAINT PK_Person PRIMARY KEY CLUSTERED,
	FirstName  nvarchar(50) NOT NULL,
	LastName   nvarchar(50) NOT NULL,
	MiddleName nvarchar(50)     NULL,
	Gender     char(1)      NOT NULL CONSTRAINT CK_Person_Gender CHECK (Gender in ('M', 'F', 'U', 'O'))
)
CREATE TABLE Doctor
(
	PersonID int          NOT NULL
		CONSTRAINT PK_Doctor        PRIMARY KEY CLUSTERED
		CONSTRAINT FK_Doctor_Person FOREIGN KEY
			REFERENCES Person ([PersonID])
			ON UPDATE CASCADE
			ON DELETE CASCADE,
	Taxonomy nvarchar(50) NOT NULL
)
CREATE TABLE Patient
(
	PersonID  int           NOT NULL
		CONSTRAINT PK_Patient        PRIMARY KEY CLUSTERED
		CONSTRAINT FK_Patient_Person FOREIGN KEY
			REFERENCES Person ([PersonID])
			ON UPDATE CASCADE
			ON DELETE CASCADE,
	Diagnosis nvarchar(256) NOT NULL
)
*/              
          }

          protected override Down() : void
          {
              drop Patient;
              drop Doctor;
              drop Person;
          }
  }
}
